package com.sun.stringescapeutils;

import okhttp3.*;

import java.io.IOException;

public class OkHttpPostTest {
    public static final MediaType JSON = MediaType
            .get("application/json;charset=utf-8");
    OkHttpClient client = new OkHttpClient();

    public String okHttpTest(String url,String json){
        RequestBody body = RequestBody.create(json,JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        String result = null;
        try(Response response = client.newCall(request).execute()) {
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    public static void main(String[] args) {
        String url = "http://localhost:8888/register";
        String json = "{\"account\":\"sun\",\"password\":\"sun\",\"nickname\":\"sun\"}";
        OkHttpPostTest okHttpPostTest = new OkHttpPostTest();
        String result =okHttpPostTest.okHttpTest(url,json);
        System.out.println(result);
    }
}
